<div id="app">
    <admin-page-header>
        <bi-button icon="fa-plus" type="primary" @click="handleEdit()">添加模型</bi-button>
    </admin-page-header>

    <div class="container">
        <div class="row g-2">
            <div class="col-12">
                <bi-card title="搜索">
                    <div class="row g-3">
                        <div class="col-12 col-sm-6 col-lg-3">
                            <bi-select v-model="search.type"  placeholder="请选择类型" >
                                <bi-select-option v-for="title,key in modelType" :key="key"  :value="key" :label="title"></bi-select-option>
                            </bi-select>
                        </div>
                        <div class="col-12 col-sm-6 col-lg-9">
                            <bi-input v-model="search.keyword"  placeholder="" icon="fa fa-search" ></bi-input>
                        </div>
                    </div>
                </bi-card>
            </div>
            <div class="col-12">
                <bi-card title="模型列表">
                    <bi-table :data="list" :loading="loading"  min-height="300" >
                        <bi-table-column label="类型" v-slot="{row}" width="100" >
                                 {{modelType[row.type]}}
                         </bi-table-column>
                        <bi-table-column label="模型名称" name="title" ></bi-table-column>
                        <bi-table-column label="操作" width="220" v-slot="{row}">
                            <bi-button type="primary" @click="handleParam(row)"  >管理参数</bi-button>
                            <bi-button type="primary" @click="handleEdit(row)"  >编辑</bi-button>
                            <bi-button type="warning" @click="handleDel(row)" >删除</bi-button>
                         </bi-table-column>
                       </bi-table>

                       <template #footer>
                        <bi-pagination v-model="page" :total="total" :count="count" ></bi-pagination>
                    </template>
                </bi-card>


            </div>
                
        </div>
    </div>
</div>

<script>
import modelEditDialog from 'model/model-edit-dialog.vue';
import paramListDialog from 'model/param-list-dialog.vue';
//import typeOption from 'model/type-option.js';
import {debounce} from '@@/admin/assets/js/global.js';
let app = admin.createApp({
    data(){

        let search  = {
            type:'',
            keyword:'',
        }

        return {
            search,
            list:[],
            loading:false,
            modelType:this.$pageData.modelType,
            page:1,
            total:0,
            count:0
        }
    },
    watch:{
        search:{
            handler:debounce(function(val){
                
                this.refresh();
            },1000),
            deep:true
        }       
    },
    methods:{
        refresh(){
            this.loading = true;
            this.$ajax({
                url:this.$url({action:'list'}),
                data:{
                    ...this.search
                },
                success:(res)=>{
                    this.list = res.list;
                    this.total = res.total;
                    this.count = res.count;
                    this.loading = false;
                    }
            });
        },
        handleDel(item){
            this.$confirm('确定要删除模型吗?',()=>{
                this.$ajax({
                    url:this.$url({action:'del'}),
                    data:{
                        model_id:item.model_id
                    },
                    success:(res)=>{
                        this.refresh();
                        this.$user.delLocalStorage('web-model-'+item.type);
                        this.$toast({text:'删除成功',type:'success'});
                    }
                })
            })
        },
        handleEdit(item){
            let title  = '添加模型';
            if (item) title  = '编辑模型';

            this.$dialog({
                title,
                type:'vue',
                content:admin.createApp(modelEditDialog,{item}),
                callback:(value)=>{
                    this.$user.delLocalStorage('web-model-'+value.type);
                    this.refresh();
                }
            });
            
        },
        handleParam(item){
        //    this.$go({action:'param',gets:{model_id:item.model_id}});
            this.$dialog({
                type:'vue',
                content:admin.createApp(paramListDialog,{model_id:item.model_id}),
                callback:()=>{
                }
            });
        }
    },
    mounted(){
        this.refresh();
    }
}).mount('#app');
</script>